package org.ztr.yanai.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import org.ztr.yanai.blog.entity.Category;
import org.ztr.yanai.blog.service.CategoryService;
import org.ztr.yanai.blog.utils.Result;

/**
 * @Author: ZhaoTR
 * @Date: Created in 2025/11/22 10:00
 * @Description: 分类管理控制器，提供分类CRUD操作
 * @Version: 1.0
 */
@RestController
@RequestMapping("/category")
@Tag(name = "分类管理", description = "分类相关的 API 接口")
public class CategoryController {

    private final CategoryService categoryService;

    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @PostMapping
    @Operation(summary = "创建分类", description = "根据传入的分类信息创建新分类")
    public Result<Category> createCategory(@RequestBody Category category) {
        boolean isSuccess = categoryService.createCategory(category);
        return isSuccess ? Result.success(category) : Result.error("创建失败");
    }

    @DeleteMapping("/{categoryId}")
    @Operation(summary = "删除分类", description = "根据分类 ID 删除分类")
    @Parameter(name = "categoryId", description = "分类 ID", required = true)
    public Result<?> deleteCategory(@PathVariable Long categoryId) {
        return categoryService.deleteCategoryById(categoryId) ? Result.success("删除分类成功！") : Result.error("删除失败");
    }

    @GetMapping("/{categoryId}")
    @Operation(summary = "获取分类详情", description = "根据分类 ID 获取分类详细信息")
    @Parameter(name = "categoryId", description = "分类 ID", required = true)
    public Result<Category> getCategoryById(@PathVariable Long categoryId) {
        return Result.success(categoryService.getCategoryById(categoryId));
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询分类", description = "支持分页和关键字查询分类列表")
    @Parameters({
            @Parameter(name = "pageNum", description = "当前页码，默认为 1", example = "1"),
            @Parameter(name = "pageSize", description = "每页显示条数，默认为 10", example = "10"),
            @Parameter(name = "keyword", description = "搜索关键字，可选", example = "技术")
    })
    public Result<Page<Category>> listCategoriesByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String keyword) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        if (keyword != null && !keyword.isEmpty()) {
            wrapper.like("name", keyword)
                    .or().like("description", keyword);
        }
        return Result.success(categoryService.listCategoriesByPage(pageNum, pageSize, wrapper));
    }

    @PutMapping
    @Operation(summary = "更新分类", description = "根据传入的分类信息更新分类数据")
    public Result<?> updateCategory(@RequestBody Category category) {
        return categoryService.updateCategory(category) ? Result.success(category) : Result.error("更新失败");
    }
}
